<template>
  <main-page>
    <div slot="body">
      <el-form :inline="true" :model="search" size="mini">
        <el-form-item>
          <el-button type="danger" @click="dialogFormVisible = true;">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%" tooltip-effect="dark">
        <el-table-column prop="id" label="编号" width="80"></el-table-column>
        <el-table-column prop="noticeTitle" label="标题"></el-table-column>
        <el-table-column prop="noticeContent" label="内容" show-overflow-tooltip></el-table-column>
        <el-table-column label="消息类型">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.noticeType == config.notice.COLL_NEWS">全员消息</el-tag>
            <el-tag v-else>会员消息</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="发送会员">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.usersVo != undefined">
              {{scope.row.usersVo.nickName}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">{{scope.row.createTime | datetime}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              title="编辑"
              size="small"
              @click="handleClick2EditById(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              title="删除"
              size="small"
              @click="handleClick2DeleteById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination :listNum="listNum" @currentChange="handleCurrentChange"></pagination>

      <el-dialog title="消息管理" :visible.sync="dialogFormVisible" width="40%">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm">
          <el-form-item label="消息类型" prop="noticeType">
            <el-radio-group v-model="form.noticeType">
              <el-radio :label="config.notice.COLL_NEWS">全员消息</el-radio>
              <el-radio :label="config.notice.SING_NEWS">会员消息</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="标题" prop="noticeTitle">
            <el-input v-model="form.noticeTitle" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="内容" prop="noticeContent">
            <el-input type="textarea" :rows="4" v-model="form.noticeContent" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="选择会员" prop="userId" v-for="item in domains" :key="item">
            <el-select
              v-model="form.userId"
              filterable
              remote
              reserve-keyword
              placeholder="请输入昵称/登录账号"
              :remote-method="remoteMethod"
            >
              <el-option
                v-for="item in userList"
                :key="item.id"
                :label="item.nickName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="onFormSubmit">提交</el-button>
        </div>
      </el-dialog>
    </div>
  </main-page>
</template>

<script>
import {
  getNoticeList,
  saveNotice,
  deleteNotice,
  updateNoticeById
} from "api/NoticeApi";

import {getUsersAllList} from 'api/UsersApi'

import { config } from "assets/js/Data";

export default {
  name: "NoticeList",
  data() {
    return {
      tableData: [],
      listNum: 1,
      search: {
        currentIndex: 1
      },
      config: config,
      dialogFormVisible: false,
      form: {
        id: "",
        noticeTitle: "",
        noticeContent: "",
        noticeType: "",
        userId: ""
      },
      rules: {
        noticeTitle: [{required: true, message: "请输入标题"}],
        noticeContent: [{required: true, message: "请输入内容"}],
        noticeType: [{required: true, message: "请选择类型"}],
        userId: [{required: true, message: "请选择会员"}]
      },
      domains: [],
      userList: []
    };
  },
  methods: {
    handleCurrentChange(page) {
      this.search.currentIndex = page;
      this.getNoticeList();
    },
    getNoticeList() {
      getNoticeList(this.search).then(res => {
        this.tableData = res.datas;
        this.listNum = res.pagination.totalCount;
      });
    },
    onFormSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id) {
            //修改
            updateNoticeById(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getNoticeList();
              }
            });
          } else {
            //保存
            saveNotice(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getNoticeList();
              }
            });
          }
        }
      });
    },
    handleClick2EditById(row) {
      this.dialogFormVisible = true;
      this.$base.set2Form(this.form, row);
    },
    handleClick2DeleteById(id) {
      this.$base.deleteConfirm(() => {
        deleteNotice({ id }).then(res => {
          if (res) {
            this.$base.deleteOk();
            this.getNoticeList();
          }
        });
      });
    },
    remoteMethod (query) {
      getUsersAllList({
        query
      }).then(res => {
        if(res) {
          this.userList = res
        }
      })
    }
  },
  created() {
    this.getNoticeList();
  },
  watch: {
    dialogFormVisible (newDialogFormVisible) {
      if (!newDialogFormVisible) {
        this.$base.clearObj(this.form);
      }
    },
    'form.noticeType' (newNoticeType) {
      if(newNoticeType == config.notice.SING_NEWS) {
        this.domains.push(1)
      } else this.domains = []
    }
  }
};
</script>

<style lang="stylus" scoped></style>